package com.qf.controller;

import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author fy
 * @version V1.0
 * @Project day41
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/5/27 12:40
 */
@WebServlet("/user")
public class UserController extends BaseServlet{
    public String login(HttpServletRequest request, HttpServletResponse response){
        //1.获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //2：判断用户名密码不为空，如果为空跳转到登陆页面
        if(username==null||password==null){
            return "redirect:" + request.getContextPath()+"/login.jsp";
        }
        //3：调用service校验用户名密码
        UserService userService = new UserServiceImpl();
        User user = userService.login(username, password);

        //4：根据结果重定向操作
        if(user !=null){
            //5：将登录成功后的对象放入tomcat的session会话中一直携带
            request.getSession().setAttribute("user", user);


            //跳转到查询分页列表
            return "redirect:"+request.getContextPath()+"/MyProfile?method=list";

        }else {
            return "redirect:"+request.getContextPath()+"/register.jsp";
        }

    }
    public String register(HttpServletRequest request, HttpServletResponse response){
        //1.获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //2：判断用户名密码不为空，如果为空跳转到注册页面
        if(username==null||password==null){
            return "redirect:" + request.getContextPath()+"/register.jsp";
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setStatus(null);

        //3：调用service添加新用户
        UserService userService = new UserServiceImpl();
        userService.addUser(user);


        //4：根据结果重定向操作

        return "redirect:"+request.getContextPath()+"/login.jsp";

    }
}
